home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 2010 Software/Programs / PCGuia_programas.iso / Software / Utils / The GIMP / gimp-2.6.8-i686-setup.exe / {app} / lib / gimp / 2.0 / plug-ins / python-console.py < prev    next >
Encoding:
Python Source  |  2009-12-15  |  8.1 KB  |  231 lines

  1. #!/usr/bin/env python
  2.  
  3. #   Gimp-Python - allows the writing of Gimp plugins in Python.
  4. #   Copyright (C) 1997  James Henstridge <james@daa.com.au>
  5. #
  6. #   This program is free software; you can redistribute it and/or modify
  7. #   it under the terms of the GNU General Public License as published by
  8. #   the Free Software Foundation; either version 2 of the License, or
  9. #   (at your option) any later version.
  10. #
  11. #   This program is distributed in the hope that it will be useful,
  12. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #   GNU General Public License for more details.
  15. #
  16. #   You should have received a copy of the GNU General Public License
  17. #   along with this program; if not, write to the Free Software
  18. #   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20. from gimpfu import *
  21.  
  22. t = gettext.translation('gimp20-python', gimp.locale_directory, fallback=True)
  23. _ = t.ugettext
  24.  
  25. PROC_NAME = 'python-fu-console'
  26.  
  27. RESPONSE_BROWSE, RESPONSE_CLEAR, RESPONSE_SAVE = range(3)
  28.  
  29. def do_console():
  30.     import pygtk
  31.     pygtk.require('2.0')
  32.  
  33.     import sys, gobject, gtk, gimpenums, gimpshelf, gimpui, pyconsole
  34.  
  35.     namespace = {'__builtins__': __builtins__,
  36.                  '__name__': '__main__', '__doc__': None,
  37.                  'gimp': gimp, 'pdb': gimp.pdb,
  38.                  'shelf': gimpshelf.shelf}
  39.  
  40.     for s in gimpenums.__dict__.keys():
  41.         if s[0] != '_':
  42.             namespace[s] = getattr(gimpenums, s)
  43.  
  44.     class GimpConsole(pyconsole.Console):
  45.         def __init__(self, quit_func=None):
  46.             banner = ('GIMP %s Python Console\nPython %s\n' %
  47.                       (gimp.pdb.gimp_version(), sys.version))
  48.             pyconsole.Console.__init__(self,
  49.                                        locals=namespace, banner=banner,
  50.                                        quit_func=quit_func)
  51.         def _commit(self):
  52.             pyconsole.Console._commit(self)
  53.             gimp.displays_flush()
  54.  
  55.     class ConsoleDialog(gimpui.Dialog):
  56.         def __init__(self):
  57.             gimpui.Dialog.__init__(self, title=_("Python Console"),
  58.                                    role=PROC_NAME, help_id=PROC_NAME,
  59.                                    buttons=(gtk.STOCK_SAVE,  RESPONSE_SAVE,
  60.                                             gtk.STOCK_CLEAR, RESPONSE_CLEAR,
  61.                                             _("_Browse..."), RESPONSE_BROWSE,
  62.                                             gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
  63.  
  64.             self.set_alternative_button_order((gtk.RESPONSE_CLOSE,
  65.                                                RESPONSE_BROWSE,
  66.                                                RESPONSE_CLEAR,
  67.                                                RESPONSE_SAVE))
  68.  
  69.             self.cons = GimpConsole(quit_func=lambda: gtk.main_quit())
  70.  
  71.             self.connect('response', self.response)
  72.  
  73.             self.browse_dlg = None
  74.             self.save_dlg = None
  75.  
  76.             vbox = gtk.VBox(False, 12)
  77.             vbox.set_border_width(12)
  78.             self.vbox.pack_start(vbox)
  79.  
  80.             scrl_win = gtk.ScrolledWindow()
  81.             scrl_win.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
  82.             vbox.pack_start(scrl_win)
  83.  
  84.             scrl_win.add(self.cons)
  85.  
  86.             self.set_default_size(500, 500)
  87.  
  88.         def response(self, dialog, response_id):
  89.             if response_id == RESPONSE_BROWSE:
  90.                 self.browse()
  91.             elif response_id == RESPONSE_CLEAR:
  92.                 self.cons.banner = None
  93.                 self.cons.clear()
  94.             elif response_id == RESPONSE_SAVE:
  95.                 self.save_dialog()
  96.             else:
  97.                 gtk.main_quit()
  98.  
  99.             self.cons.grab_focus()
  100.  
  101.         def browse_response(self, dlg, response_id):
  102.             if response_id != gtk.RESPONSE_APPLY:
  103.                 dlg.hide()
  104.                 return
  105.  
  106.             proc_name = dlg.get_selected()
  107.  
  108.             if not proc_name:
  109.                 return
  110.  
  111.             proc = pdb[proc_name]
  112.  
  113.             cmd = ''
  114.  
  115.             if len(proc.return_vals) > 0:
  116.                 cmd = ', '.join([x[1].replace('-', '_')
  117.                                 for x in proc.return_vals]) + ' = '
  118.  
  119.             cmd = cmd + 'pdb.%s' % proc.proc_name.replace('-', '_')
  120.  
  121.             if len(proc.params) > 0 and proc.params[0][1] == 'run-mode':
  122.                 params = proc.params[1:]
  123.             else:
  124.                 params = proc.params
  125.  
  126.             cmd = cmd + '(%s)' % ', '.join([x[1].replace('-', '_')
  127.                                            for x in params])
  128.  
  129.             buffer = self.cons.buffer
  130.  
  131.             lines = buffer.get_line_count()
  132.             iter = buffer.get_iter_at_line_offset(lines - 1, 4)
  133.             buffer.delete(iter, buffer.get_end_iter())
  134.             buffer.place_cursor(buffer.get_end_iter())
  135.             buffer.insert_at_cursor(cmd)
  136.  
  137.         def browse(self):
  138.             if not self.browse_dlg:
  139.                 dlg = gimpui.ProcBrowserDialog(_("Python Procedure Browser"),
  140.                                                role=PROC_NAME,
  141.                                                buttons=(gtk.STOCK_APPLY,
  142.                                                         gtk.RESPONSE_APPLY,
  143.                                                         gtk.STOCK_CLOSE,
  144.                                                         gtk.RESPONSE_CLOSE))
  145.  
  146.                 dlg.set_default_response(gtk.RESPONSE_APPLY)
  147.                 dlg.set_alternative_button_order((gtk.RESPONSE_CLOSE,
  148.                                                   gtk.RESPONSE_APPLY))
  149.  
  150.                 dlg.connect('response', self.browse_response)
  151.                 dlg.connect('row-activated',
  152.                             lambda dlg: dlg.response(gtk.RESPONSE_APPLY))
  153.  
  154.                 self.browse_dlg = dlg
  155.  
  156.             self.browse_dlg.present()
  157.  
  158.         def save_response(self, dlg, response_id):
  159.             if response_id == gtk.RESPONSE_DELETE_EVENT:
  160.                 self.save_dlg = None
  161.                 return
  162.             elif response_id == gtk.RESPONSE_OK:
  163.                 filename = dlg.get_filename()
  164.  
  165.                 try:
  166.                     logfile = open(filename, 'w')
  167.                 except IOError, e:
  168.                     gimp.message(_("Could not open '%s' for writing: %s") %
  169.                                  (filename, e.strerror))
  170.                     return
  171.  
  172.                 buffer = self.cons.buffer
  173.  
  174.                 start = buffer.get_start_iter()
  175.                 end = buffer.get_end_iter()
  176.  
  177.                 log = buffer.get_text(start, end, False)
  178.  
  179.                 try:
  180.                     logfile.write(log)
  181.                     logfile.close()
  182.                 except IOError, e:
  183.                     gimp.message(_("Could not write to '%s': %s") %
  184.                                  (filename, e.strerror))
  185.                     return
  186.  
  187.             dlg.hide()
  188.  
  189.         def save_dialog(self):
  190.             if not self.save_dlg:
  191.                 dlg = gtk.FileChooserDialog(_("Save Python-Fu Console Output"),
  192.                                             parent=self,
  193.                                             action=gtk.FILE_CHOOSER_ACTION_SAVE,
  194.                                             buttons=(gtk.STOCK_CANCEL,
  195.                                                      gtk.RESPONSE_CANCEL,
  196.                                                      gtk.STOCK_SAVE,
  197.                                                      gtk.RESPONSE_OK))
  198.  
  199.                 dlg.set_default_response(gtk.RESPONSE_OK)
  200.                 dlg.set_alternative_button_order((gtk.RESPONSE_OK,
  201.                                                   gtk.RESPONSE_CANCEL))
  202.  
  203.                 dlg.connect('response', self.save_response)
  204.  
  205.                 self.save_dlg = dlg
  206.  
  207.             self.save_dlg.present()
  208.  
  209.         def run(self):
  210.             self.show_all()
  211.             gtk.main()
  212.  
  213.     ConsoleDialog().run()
  214.  
  215. register(
  216.     PROC_NAME,
  217.     N_("Interactive GIMP Python interpreter"),
  218.     "Type in commands and see results",
  219.     "James Henstridge",
  220.     "James Henstridge",
  221.     "1997-1999",
  222.     N_("_Console"),
  223.     "",
  224.     [],
  225.     [],
  226.     do_console,
  227.     menu="<Image>/Filters/Languages/Python-Fu",
  228.     domain=("gimp20-python", gimp.locale_directory))
  229.  
  230. main()
  231.